package com.gxm.练习.多线程.线程池;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author 郭训民
 * @description: TODO
 * @title: FutureDemo
 * @projectName jsee
 * @date 2021/7/17 9:02
 */
public class FutureDemo {

    @Test
    public void test01() throws InterruptedException, ExecutionException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        List<Future> results = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            TaskCallable task = new TaskCallable(String.valueOf(i));
            results.add(executorService.submit(task));
        }
        executorService.shutdown();
        for (Future searchResponseFuture: results){
            while (true) {
                if(searchResponseFuture.isDone() && !searchResponseFuture.isCancelled()) {
                    System.out.println(searchResponseFuture.get());
                    break;
                } else {
                    Thread.sleep(100);
                }
            }
        }
        System.out.println("结束");
    }

    @Test
    public void test02(){
        double subListSize =  Math.ceil(19.8);
        System.out.println(subListSize);
        Map<String, String> map = Maps.newHashMapWithExpectedSize(3);
        map.put("1","1");
        map.put("2","1");
        map.put("3","1");
        System.out.println(map.keySet().contains("11"));
    }
}
